package com.sanyang.pda.ui.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.bean.StorageInfoBean;
import com.sanyang.pda.contract.HomeContract;
import com.sanyang.pda.presenter.HomePresenter;
import com.sanyang.pda.utils.UserUtils;
import com.sanyang.pda.view.ScanButView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.BaseUrl;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.SharedPreferencesUtil;
import com.wangshen.projectbase.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.HOMEACTIVITY)
public class HomeActivity extends BaseMvpActivity<HomePresenter> implements HomeContract.View, OnRefreshListener {
    @BindView(R.id.iv_admin)
    ImageView ivAdmin;
    @BindView(R.id.tv_admin)
    TextView tvAdmin;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.switch_warehouse)
    RelativeLayout switchWarehouse;
    @BindView(R.id.tv_total_location)
    TextView tvTotalLocation;
    @BindView(R.id.tv_used)
    TextView tvUsed;
    @BindView(R.id.tv_occupancy_rate)
    TextView tvOccupancyRate;
    @BindView(R.id.ll_manual_pallet)
    LinearLayout llManualPallet;
    @BindView(R.id.ll_spot_check)
    LinearLayout llSpotCheck;
    @BindView(R.id.ll_sampling_inspection_results)
    LinearLayout llSamplingInspectionResults;
    @BindView(R.id.ll_manual_inventory)
    LinearLayout llManualInventory;
    @BindView(R.id.ll_manual_outbound)
    LinearLayout llManualOutbound;
    @BindView(R.id.location_query)
    ImageView locationQuery;
    @BindView(R.id.ll_stock_taking)
    LinearLayout llStockTaking;
    @BindView(R.id.ll_scan_code_query)
    LinearLayout llScanCodeQuery;
    @BindView(R.id.ll_location_query)
    LinearLayout llLocationQuery;
  /*  @BindView(R.id.search)
    ScanButView search;*/
    @BindView(R.id.ll_manual_picking)
    LinearLayout llManualPicking;
    @BindView(R.id.ll_library_of_warehouse)
    LinearLayout llLibraryOfWarehouse;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;
    @BindView(R.id.ll_movement)
    LinearLayout llMovement;
    @BindView(R.id.ll_putaway)
    LinearLayout llPutaway;
    @BindView(R.id.ll_failed_manager)
    LinearLayout llFailedManager;
    @BindView(R.id.ll_scan_demo)
    LinearLayout llScanDemo;

    @Override
    public HomePresenter getPresenter() {
        return new HomePresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_home;
    }

    @Override
    public void initView() {
        smart.setOnRefreshListener(this);
        smart.setEnableRefresh(false);
        smart.setEnableLoadMore(false);
        //tvAdmin.setText(UserUtils.getInstance().getUserBean().getUserName());
        ClassicsHeader.REFRESH_HEADER_REFRESHING = getString(R.string.is_refreshing);
        ClassicsHeader.REFRESH_HEADER_LOADING = getString(R.string.loading);
        ClassicsHeader.REFRESH_HEADER_FINISH = getString(R.string.refresh_to_complete);
        ClassicsHeader.REFRESH_HEADER_LASTTIME = getString(R.string.last_update);
        ClassicsFooter.REFRESH_FOOTER_PULLUP = getString(R.string.pull_up_to_load_more);
        ClassicsFooter.REFRESH_FOOTER_LOADING = getString(R.string.loading);
        ClassicsFooter.REFRESH_FOOTER_FINISH = getString(R.string.loading_completed);
        ClassicsFooter.REFRESH_FOOTER_ALLLOADED = getString(R.string.no_more_data);
    }

    @Override
    public void initLinsenterner() {

    }

    @Override
    public void getData() {
       // presenter.storageUseInfo();
        String ip=SharedPreferencesUtil.getInstance().getString("ip", "");

        if (TextUtils.isEmpty(ip)){
            showMessage("请先设置服务器地址");
            startIntent(AppActivityKey.SETACTIVITY);
        }else {
            BaseUrl.BASEURL="http://"+ SharedPreferencesUtil.getInstance().getString("ip", "");
        }
    }

    @OnClick({R.id.ll_manual_pallet, R.id.ll_spot_check, R.id.ll_sampling_inspection_results, R.id.ll_manual_inventory, R.id.ll_manual_outbound, R.id.ll_stock_taking, R.id.ll_scan_code_query, R.id.ll_location_query})
    public void onViewClicked(View view) {
        if (TextUtils.isEmpty(BaseUrl.BASEURL)){
            showMessage("请先设置服务器地址");
            startIntent(AppActivityKey.SETACTIVITY);
        }
        switch (view.getId()) {
            case R.id.ll_manual_pallet:
                startIntent(AppActivityKey.LSSUEDWORKACTIVITY);
                break;
            case R.id.ll_spot_check:
                startIntent(AppActivityKey.LSSUEDSETACTIVITY);
                break;
            case R.id.ll_sampling_inspection_results:
                startIntent(AppActivityKey.SETACTIVITY);
                break;
            case R.id.ll_manual_inventory:
                startIntent(AppActivityKey.MANUALDELIVERYACTIVITY);
                break;
            case R.id.ll_manual_outbound:
                startIntent(AppActivityKey.MANUALOUTBOUNDACTIVITY);
                break;

            case R.id.ll_stock_taking:
                startIntent(AppActivityKey.TAKESTOCKLISTACTIVITY);
                break;
            case R.id.ll_scan_code_query:
                startIntent(AppActivityKey.REPLACELISTACTIVITY);
                break;
            case R.id.ll_location_query:
                startIntent(AppActivityKey.STOCKSEARCHACTIVITY);
                break;
        }
    }

    @Override
    public void setStorageUseInfo(StorageInfoBean storageInfoBean) {
        smart.finishRefresh();
        tvTotalLocation.setText(getResources().getString(R.string.overall_seat) + storageInfoBean.getTotal());
        tvUsed.setText(getResources().getString(R.string.have_been_used) + storageInfoBean.getUsedNum());
        tvOccupancyRate.setText(getResources().getString(R.string.occupancy_rate) + storageInfoBean.getUsage());
    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        smart.finishRefresh();
    }

    @OnClick(R.id.ll_manual_picking)
    public void onLlManualPickingClicked() {
        startIntent(AppActivityKey.MANUALPICKINGACTIVITY);
    }

    @OnClick(R.id.ll_library_of_warehouse)
    public void onLlLibraryOfWarehouseClicked() {
        startIntent(AppActivityKey.CROSSDOCKINGACTYIVITY);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
       // presenter.storageUseInfo();
    }




    @OnClick(R.id.ll_movement)
    public void onViewClicked() {
        startIntent(AppActivityKey.MOVEMENTACTIVITY);
    }


    @OnClick({R.id.ll_putaway, R.id.ll_failed_manager})
    public void onViewClicked1(View view) {
        switch (view.getId()) {
            case R.id.ll_putaway:
                startIntent(AppActivityKey.PUTAWAYLISTACTIVITY);
                break;
            case R.id.ll_failed_manager:
                startIntent(AppActivityKey.FAILEDMANAGERACTIVITY);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.ll_scan_demo)
    public void onViewClicked12() {
        startIntent(AppActivityKey.SCANDEMOACIVITY);
    }
}
